<template>
      <li @click.stop='toggle' >
          <a :href="menuHtml" :class="{collapsed:isopen,childMenuSelected:isChoose}">
            {{menuItemsData.menuTitle}}<i class="glyphicon" :class="{'arrow':isChildNode}"></i>
          </a>
          <ul v-if='isChildNode' class="listPanel" :class='[menuItemsData.menuClassLevel,{listPanelHide:!isopen,listPanelShow:isopen}]'>
              <menu-items v-for='cel in menuItemsData.menuChild' :menuItemsData='cel'></menu-items>
          </ul>
      </li>
</template>

<script type="text/javascript">
import {commonVue} from '../assets/js/base';
var _self;

  export default {
    name: 'menu-items',
    props: ['menuItemsData'],
    data() {
       return {
           isopen: false,
           isChoose:false,
           pageInitOver:false
       }
    },
    computed: {
      isChildNode: function () {
        return this.menuItemsData.menuChild && this.menuItemsData.menuChild.length
      },
      menuHtml: function () {
        return !!this.menuItemsData.menuSrc ? this.menuItemsData.menuSrc : "#"
      }
    },
    created:function(){
      _self = this;
      setCurrentMenuStatue();
    },
    methods: {
        toggle: function() {
            if(this.$children.length == 0){
              commonVue.$emit('menuSelected',this);
              this.isChoose = true;
            }else{
              commonVue.$emit('menuClicked',this._uid,this);
            }
            this.isopen = !this.isopen
        }
    }
  }

  function setCurrentMenuStatue(){
    if(!!_self.menuItemsData.menuSrc){
      if(_self.menuItemsData.menuSrc == _self.$root.currentMenu){
        _self.isopen = true;
        _self.isChoose = true;
        setCurrentMenuParentStatue(_self);
      }
    }
  }

  function setCurrentMenuParentStatue(menuTree){
    if(!!menuTree.$parent){
      menuTree.$parent.isopen = true;
      if(_self.pageInitOver == false){//用于设置页面一加载手机端默认不显示菜单
        _self.pageInitOver = true;
        return;
      }
      setCurrentMenuParentStatue(menuTree.$parent);
    }
  }
</script>
